package facadeDesignPattern;

/**
 * @author Anitta 
 * 
 * Menu class provides a widow for client with necessary methods. It uses methods defined in Function class.
 */

import iteratorDesignPattern.EmployeeRepository;
import iteratorDesignPattern.Iterator;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import thread.SaveThread;
import classes.Employee;
import filterDesignPattern.YoungCriteria;

public class Menu extends javax.swing.JFrame {

	List<Employee> employList = new ArrayList<Employee>();
    /**
     * Creates new form Menu
     */
    public Menu() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        FNameField = new javax.swing.JTextField();
        LNameField = new javax.swing.JTextField();
        AgeField = new javax.swing.JTextField();
        CityField = new javax.swing.JTextField();
        DepartmentField = new javax.swing.JTextField();
        FunctionField = new javax.swing.JTextField();
        SalaryField = new javax.swing.JTextField();
        AddButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        SubmitEmplButton = new javax.swing.JButton();
        ClearEmployeeButton = new javax.swing.JButton();
        ViewAllButton = new javax.swing.JButton();
        YoungAdultButton = new javax.swing.JButton();
        AdultButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Employees App");
        setResizable(false);

        jLabel1.setText("First Name:");

        jLabel2.setText("Last Name:");

        jLabel3.setText("Age:");

        jLabel4.setText("City:");

        jLabel5.setText("Department:");

        jLabel6.setText("Function:");

        jLabel7.setText("Salary:");

        FNameField.setToolTipText("");
        FNameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FNameFieldActionPerformed(evt);
            }
        });

        AddButton.setText("Add Employee");
        AddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddButtonActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        jScrollPane1.setViewportView(jTextArea1);

        SubmitEmplButton.setText("Submit Employees");
        SubmitEmplButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SubmitEmplButtonActionPerformed(evt);
            }
        });

        ClearEmployeeButton.setText("Clear Employees");
        ClearEmployeeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ClearEmployeeButtonActionPerformed(evt);
            }
        });

        ViewAllButton.setText("View All Employees");
        ViewAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ViewAllButtonActionPerformed(evt);
            }
        });

        YoungAdultButton.setText("View Young Adult");
        YoungAdultButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                YoungAdultButtonActionPerformed(evt);
            }
        });

        AdultButton.setText("View Adult");
        AdultButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AdultButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(77, 77, 77)
                        .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(LNameField, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                                .addComponent(AgeField, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                                .addComponent(CityField, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                                .addComponent(DepartmentField, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                                .addComponent(FunctionField, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                                .addComponent(SalaryField, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE))
                            .addComponent(FNameField, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 423, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(AdultButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(ViewAllButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(YoungAdultButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(ClearEmployeeButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(SubmitEmplButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(38, 38, 38)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1)
                                .addComponent(FNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2)
                                .addComponent(LNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(AgeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(4, 4, 4)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(CityField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel4))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel5)
                                .addComponent(DepartmentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel6)
                                .addComponent(FunctionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(SalaryField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel7))
                            .addGap(44, 44, 44)
                            .addComponent(AddButton)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(62, 62, 62)
                        .addComponent(SubmitEmplButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ClearEmployeeButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ViewAllButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(YoungAdultButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(AdultButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(73, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void FNameFieldActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    // This method saves the Employee object in a list of Employee object and shows the list in a JTextArea
    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	try {
			Employee e = new Employee();
			if ((FNameField.getText().trim().isEmpty())
					|| (LNameField.getText().trim().isEmpty())
					|| (AgeField.getText().trim().isEmpty())
					|| (CityField.getText().trim().isEmpty())
					|| (FunctionField.getText().trim().isEmpty())
					|| (DepartmentField.getText().trim().isEmpty())
					|| (SalaryField.getText().trim().isEmpty())) {
				JOptionPane jp = new JOptionPane();
				JOptionPane.showMessageDialog(jp, "Fields cannot be empty!");
			} else {

				e.setFirstName(FNameField.getText());
				e.setLastName(LNameField.getText());
				e.setAge(Integer.parseInt(AgeField.getText()));
				e.setCity(CityField.getText());
				e.setDepartment(DepartmentField.getText());
				e.setFunction(FunctionField.getText());
				e.setSalary(Float.parseFloat(SalaryField.getText()));
				employList.add(e);
				jTextArea1.append(e.toString2());
			}
    	}catch (Exception e) {
			e.printStackTrace();
		}

		finally {

			LNameField.setText(null);
			FNameField.setText(null);
			AgeField.setText(null);
			CityField.setText(null);
			DepartmentField.setText(null);
			FunctionField.setText(null);
			SalaryField.setText(null);
		}
    }
    // end of AddButtonAction
    
    // This method saves the list of Employee object in a file using Threads
	private void SubmitEmplButtonActionPerformed(java.awt.event.ActionEvent evt) {
		SaveThread toSaveThread = new SaveThread();
		if (employList.size() == 0) {
			JOptionPane jp = new JOptionPane();
			JOptionPane.showMessageDialog(jp, "Nothing to save!");
		} else {
			toSaveThread.employeeList = employList;
			Thread theThread = new Thread(toSaveThread);
			theThread.start();
			JOptionPane jp = new JOptionPane();
			JOptionPane.showMessageDialog(jp, "Success!");
			employList.clear();
		}

		// without threading
//		Functions toWrite = new Functions();
//		try {
//			if (employList.size() == 0) {
//				JOptionPane jp = new JOptionPane();
//				JOptionPane.showMessageDialog(jp, "Nothing to save!");
//			} else {
//				toWrite.write(employList);
//				employList.clear();
//				JOptionPane jp = new JOptionPane();
//				JOptionPane.showMessageDialog(jp, "Success!");
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
	}
	// end of SubmitButtonAction
    
    // This method clears the list of Employee and the JTextArea
    private void ClearEmployeeButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	employList.clear();
		jTextArea1.setText("");
    }
    // end of ClearAction
    
    // This method shows in the JTextArea all the Employees saved in file. It uses methods defined in Iterator Design Pattern
    private void ViewAllButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	jTextArea1.setText("");
    	EmployeeRepository employeeRep= new EmployeeRepository();
    	for (Iterator i= employeeRep.getIterator(); i.hasNext();){
    		Employee e = new Employee();
    		e=(Employee)i.next();
    		jTextArea1.append(e.toString2());
    	}
    }
    // end of ViewAllAction
    
    // Methods using Criteria Design Pattern
	private void YoungAdultButtonActionPerformed(java.awt.event.ActionEvent evt) {
		jTextArea1.setText("");
		Functions f = new Functions();
		List<Employee> youngList = f.youngCriteria();
		if (youngList.size() == 0) {
			jTextArea1.append("Nothing to show!");
		} else {
			for (Employee e : youngList) {
				jTextArea1.append(e.toString2());
			}
		}

	}
    // end of YoungCriteriaAction
    
	private void AdultButtonActionPerformed(java.awt.event.ActionEvent evt) {
		jTextArea1.setText("");
		Functions f = new Functions();
		List<Employee> adultList = f.adultCriteria();
		if (adultList.size() == 0) {
			jTextArea1.append("Nothing to show!");
		} else {
			for (Employee e : adultList)
				jTextArea1.append(e.toString2());
		}
	}
    //end of AdultCriteriaAction
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Menu().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton AddButton;
    private javax.swing.JButton AdultButton;
    private javax.swing.JTextField AgeField;
    private javax.swing.JTextField CityField;
    private javax.swing.JButton ClearEmployeeButton;
    private javax.swing.JTextField DepartmentField;
    private javax.swing.JTextField FNameField;
    private javax.swing.JTextField FunctionField;
    private javax.swing.JTextField LNameField;
    private javax.swing.JTextField SalaryField;
    private javax.swing.JButton SubmitEmplButton;
    private javax.swing.JButton ViewAllButton;
    private javax.swing.JButton YoungAdultButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration
}